package at.codebase.core.search;

import java.util.ArrayList;

import at.codebase.core.search.SortOption.SortOptionDetail;
import at.codebase.core.search.SortOption.SortOptionType;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;
import static at.codebase.web.util.i18n.I18NFactory.i18n;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
public class SortOptionsFormFactory {

	public static Template createSortOptions(RequestInformation reqInfo, final String query, ArrayList<SortOption> sortOptions) {
		
		Template sortContainer = TemplateEngine.getTemplate("search/sort_options_container.html");
		sortContainer.setText("query", query);
		
		ArrayList<Template> sortOptionsTemplates = new ArrayList<Template>();
		
		//Sort Option Template
		Template templateOption = TemplateEngine.getTemplate("search/sort_option.html");
		templateOption.setText("templateHidden", "display:none;");
		templateOption.setText("number", "template");
		String defaultType = SortOption.getStringType(SortOptionType.TITLE);
		String defaultDetail = SortOption.getStringDetail(SortOptionDetail.ASCENDING);
		templateOption.setText("selected_"+defaultType+"_"+defaultDetail, "selected=\"selected\"");
		sortOptionsTemplates.add(templateOption);
		
		int c = 1;
		for(SortOption option:sortOptions){
			Template sortOption = TemplateEngine.getTemplate("search/sort_option.html");
			
			sortOption.setText("number", String.valueOf(c));
			String type = SortOption.getStringType(option.getType());
			String detail = SortOption.getStringDetail(option.getDetail());
			sortOption.setText("selected_"+type+"_"+detail, "selected=\"selected\"");
			
			sortOptionsTemplates.add(sortOption);
			c++;
		}
		
		if(c == 1){
			Template sortOption = TemplateEngine.getTemplate("search/sort_option.html");
			sortOption.setHTML("defaultOption", "<option value=\"\" selected=\"selected\">"+i18n(reqInfo.getLocale()).string("search_sort_option_pleaseChoose")+"</option>");
			sortOption.setText("number", String.valueOf(c));
			sortOptionsTemplates.add(sortOption);
		}
		
		sortContainer.setTemplates("sortOptions", sortOptionsTemplates);
		
		return sortContainer;
	}

}
